<?php
/**
 * unit test case for pathnode common libraries :
 * Pathnode, FolderNode and Filenode
 */
class TestPathNode extends UnitTestCase {

    function setUp() {
        // deleting test file
        // unlink(dirname(__FILE__).'/tmp.txt');
    }

    function testGetFile() {
        // echo $self_dir;
        $file=Pathnode::getnode(__FILE__);
        $this->assertNoErrors();
        $this->assertIsA($file,'filenode');
        $this->assertTrue($file->exists(),'pathnode->exists() should return true if file exists');
        $this->assertEqual($file->getbasename(),basename(__FILE__),'pathnode->getbasename should return fiel base name');
        $this->assertEqual($file->getAbsolutePath(),__FILE__);
        $this->assertEqual($file->getRelativePath(),substr(__FILE__,strlen(dirname($_SERVER['SCRIPT_FILENAME']))+1));
    }

    function testGetNull() {
        $tmp=Pathnode::getnode(dirname(__FILE__).'/tmp');
        $this->assertNoErrors();
        $this->assertNull($tmp,'pathnode::getnode should return null for non-existing nodes');
    }

    function testFolder() {
        $f=Pathnode::getnode(dirname(__FILE__).'/un dossier');
        $this->assertIsA($f,'Foldernode','pathnode::getnode should get a Foldernode when path is a folder');

        $files=$f->getChildren();
        $this->assertIsA($files,'array','Foldernode->getChildren() should return an array');
        foreach ($files as $file) {
            $this->assertIsA($file,'Pathnode','Foldernode->getChildren() should return an array, of Pathnode objects');
        }

        $this->dump($f);
    }

    function testFileCycle() {
        // creating a non-existing file instance
        clearstatcache();
        $file=dirname(__FILE__).'/tmp.txt';
        if (!$this->assertFalse(file_exists($file),'file '.$file.' should not exist before tests %s')) {
            return false;
        }
        $tmp=new fileNode($file); // echo $tmp->path;
        // trying read/info functions on a non-existing file
        if (!$this->assertFalse($tmp->exists(),'filenode->exists() should return false for non-existing files %s')
        or !$this->assertFalse($tmp->delete(),'filenode->delete() should return false if file dont exist %s')
        or !$this->assertFalse($tmp->isReadable(),'filenode->isReadable() should return false for non-existing files %s')
        or !$this->assertFalse($tmp->read(),'filenode->read() should return false for non-existing files %s')
        or !$this->assertFalse($tmp->isWritable(),'filenode->isWritable() should return false for non-existing files %s')
        or !$this->assertEqual($tmp->size(),0,'filenode->size() should return zero for non-existing files %s')
        or !$this->assertFalse($tmp->move(dirname(__FILE__).'/new.txt'),'filenode->move() should return false for non-existing files %s')
        // checking the file was not created by previous calls
        or !$this->assertFalse(file_exists($file),'file '.$file.' should still not exist at this point %s')) {
            $this->dump($tmp);
            return false;
        }
        // creating the file
        if (!$this->assertTrue($tmp->create(),'filenode->create() should return true when creating a non-existing file %s')
        or !$this->assertTrue(file_exists($file),'filenode->create() should create the file %s')) {

            return false;
        }
        clearstatcache();

        // moving/renaming test
        $this->assertTrue($tmp->rename('renamed.txt'),'filenode->rename() should return true');

        $this->assertTrue($tmp->move(dirname(__FILE__).'/moved.txt'),'filenode->move() should return true');

        // print_r($tmp);
        // deleting
        $this->assertTrue($tmp->delete(),'filenode->delete() should return true for existing file');
        $this->assertFalse(file_exists($file),'filenode->delete() should destroy the file');

        // $this->dump($tmp);
    }
}
?>